package demo1;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;

public class Test {
    public static void main(String[] args) {
        //关于双向链表的使用
        LinkedList<Integer> linkedList = new LinkedList<Integer>();
        linkedList.addLast(1);
        linkedList.addLast(2);
        linkedList.addLast(3);
        linkedList.addLast(4);
        //此时toString方法一定被重写了
        System.out.println("直接打印..");
        System.out.println(linkedList);

        //关于linklist的遍历
        System.out.println(".........通过for语句............");
        for (int i = 0; i < linkedList.size(); i++) {
            System.out.print(linkedList.get(i));
        }
        System.out.println();
        System.out.println("...........通过foreach语句........");
        for (  Integer i :linkedList) {   //遍历linkedList中的数据，依次放入到变量i当中去
            System.out.print(i);
        }
        System.out.println();
        System.out.println(".........Iterator迭代器..........");

        Iterator <Integer>it  =  linkedList.iterator(); //此方法用于获取一个迭代器对象
        while (it.hasNext()){ //如果有下一个节点
            System.out.print(it.next() + "  "); //则打印下一个节点,it.next()方法同时会使中的it向后走一步，指向下一个数据
        }
        System.out.println();
        System.out.println("..........ListIterator迭代器...........");

        //他继承Iterator类
        ListIterator <Integer>it2   =     linkedList.listIterator();
        while (it2.hasNext()){
            System.out.print(it2.next() + "  ");
        }
        System.out.println();
        System.out.println("........重写后的方法，从指定位置开始打印.......");
     //关于linkedList中listInterator重写的方法
        ListIterator <Integer>it3   =     linkedList.listIterator(2);
        while (it3.hasNext()){
            System.out.print(it3.next() + "  ");
        }
        System.out.println();
        System.out.println("从后往前遍历");
    //也可以从后往前打印
        ListIterator <Integer> it4 = linkedList.listIterator(linkedList.size());
        while (it4.hasPrevious()){
            System.out.print(it4.previous() + "  ");
        }



    }
    public static void main1(String[] args) {
        //接下来的任务，1. 实现昨天剩余的两道算法题
        //2. 实现双链表
        //3. 使用java提供的双向链表
        MyLinkedList myLinkedList = new MyLinkedList();
        myLinkedList.addIndex(0,1);
        myLinkedList.addIndex(1,1);
        myLinkedList.addIndex(2,3);
        myLinkedList.addIndex(3,3);
        myLinkedList.addIndex(4,5);
        myLinkedList.removeAllKey(3);
        myLinkedList.display();
    }















}
